﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Hi_Baidu.Packet.InPacket
{
    /// <summary>
    ///  * 获取分组信息反馈包
    ///  * 基础包结构 
    ///  * 用户名 : String
    ///  * 用户分组名称 : String[]
    ///  * 用户分组Id : String[]
    /// </summary>
    class GetGroupReplyPacket : BaseInPackage
    {
        public GetGroupReplyPacket(InPackage inpackage) 
            : base(inpackage)
        {
        }

        public override void InitPackage()
        {
            int Offset = 0;
            //用户名长度
            byte[] UsernameLength = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, UsernameLength, 0, 4);
            Offset += 4;
            //用户名
            byte[] Username = new byte[BitConverter.ToInt32(UsernameLength, 0)];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, Username, 0, Username.Length);
            Offset += Username.Length;
            //分组ID长度
            byte[] ItemIdLength = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, ItemIdLength, 0, 4);
            Offset += 4;
            //分组ID
            byte[] ItemId = new byte[BitConverter.ToInt32(ItemIdLength, 0)];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, ItemId, 0, ItemId.Length);
            Offset += ItemId.Length;
            //分组名称长度
            byte[] ItemNameLength = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, ItemNameLength, 0, 4);
            Offset += 4;
            //分组ID
            byte[] ItemName = new byte[BitConverter.ToInt32(ItemNameLength, 0)];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, ItemName, 0, ItemName.Length);
            Offset += ItemName.Length;

            this.DIC_CONTENT.Add("USERNAME", Username);
            this.DIC_CONTENT.Add("ITEMSID", ItemId);
            this.DIC_CONTENT.Add("ITEMS", ItemName);
        }

        /// <summary>
        ///     当前分组名称集合
        /// </summary>
        /// <returns>
        ///     返回分组名称集合
        /// </returns>
        public String[] GetItems()
        {
            return Encoding.Default.GetString(this.DIC_CONTENT["ITEMS"]).Split(new String[] {","},
                                                                                             StringSplitOptions.
                                                                                                 RemoveEmptyEntries);
        }
        /// <summary>
        ///     当前分组Id集合
        /// </summary>
        /// <returns>
        ///     返回分组Id集合
        /// </returns>
        public String[] GetItemsId()
        {
            return Encoding.Default.GetString(this.DIC_CONTENT["ITEMSID"]).Split(new String[] { "," },
                                                                                             StringSplitOptions.
                                                                                                 RemoveEmptyEntries);
        }
    }
}
